import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'

//
import perm from './common/js/permission'

//引入axios
import axios from "axios";
//配置axios的基础路径
axios.defaults.baseURL='http://localhost:8080/'
//配置Vue的全局属性:用来获取axios对象
Vue.prototype.$http = axios;


Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

//=============================axios前置拦截器【拦截调用后端的请求】=============================//
//所有的axios请求都经过，携带token给请求头
axios.interceptors.request.use(res=>{
  //获取localStorage的token
  let token = localStorage.getItem("token");
  //判断是否为空
  if(token){
    //不为空设置到请求头
    res.headers["token"] = token;
  }
  return res;
},error => {
  Promise.reject(error)
})
//=============================axios前置拦截器=============================//



//=============================axios后置拦截器【处理后台登录拦截的结果】=============================//
axios.interceptors.response.use(res=>{
  if (res.data.success === false && res.data.message === "noLogin") { //没有登录或者过了30分钟
    //清空localStorage
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("permissions");
    localStorage.removeItem("menus");
    //跳转到登录页面
    router.push({path:'/login'});
  }else if (res.data.success === false && res.data.message === "noPermission") {
    alert("无权限！");
  }else if (res.data.success === false && res.data.message === "timeout") {
    //清空localStorage
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("permissions");
    localStorage.removeItem("menus");
    alert("jwt过期");
  }
  return res;
},error => {
  Promise.reject(error)
})
//=============================axios后置拦截器=============================//



//=============================路由前置拦截器=============================//

//路由前置拦截器：每一次经过路由的请求之前都要去执行以下逻辑
router.beforeEach((to, from, next) => {
  //1.只有是登录和注册的不用发axios请求的只通过路由访问的页面才放行
  if (to.path == "/login" || to.path == "/register" || to.path == "/activate/") {
    //放行
    next();
  }else {
    //2.获取登录信息对象，判断是否有
    var logininfo = localStorage.getItem("logininfo");
    if (logininfo) {
      //有登录信息对象表示登录了
      next();
    }else { //表示既不是登录和注册页面有没有登录过或者过期了
      //跳转到登录页面
      next({path:'/login'});
    }
  }

})


//处理页面刷新动态路由失效问题
initIndexRouters();
function initIndexRouters(){
  if(!localStorage.menus){
    return;
  }
  //防止重复配置路由
  if(router.options.routes.length>6){
    return;
  }
  let menus = localStorage.getItem('menus');
  menus = JSON.parse(menus);
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      iconCls: menu.icon,
      name: menu.name,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  //动态路由配置
  router.addRoutes(tempRouters);
}

//路由后置拦截器：每一次通过路由的请求之后都要去执行以下逻辑
//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')



import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  /* Visit http://lbsyun.baidu.com/apiconsole/key for details about app key. */
  ak: 'TkNHCyXwXEpVGVsVd8hSKO7FoDkBD1UC'
})
